﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using Facade;
using System.Collections;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class ManageUsers : SOSecurityPage, IDataPage ,IntTest
    {
        //System.Web.UI.Page
        //private string usrType = "unit";
        private string usrType = "HQ";
        private string userName;
        private int isChecked = 0;
        private static int grFlag = 0;
        private static int brFlag = 0;
        private static int unFlag = 0;
        private int pageNo = 1;
        private static int u;
        private static int n;

        protected void Page_Load(object sender, EventArgs e)
        {


            UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "Id",
                    ColumnHeader = "Id",
                    ColumnWidth = 130 },
                new GridViewStyle{
                    ColumnId = "Name",
                    ColumnHeader = "Name",
                   ColumnWidth = 130},                
                new GridViewStyle{
                    ColumnId = "Last Updated By",
                    ColumnHeader = "Last Updated By",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "Last Updated Date",
                    ColumnHeader = "Last Updated Date",
                    ColumnWidth = 130}
                
            };


            try
            {
                if (!IsPostBack)
                {
                    //UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                }
                
                if (Session["uInSuc"] != null)
                {
                    if (Session["uInSuc"].Equals("1"))
                    {
                        MassageSummery("IIS401");
                    }
                    else if (Session["uInSuc"].Equals("0"))
                    {
                        MassageSummery("IIS502");
                    }
                    else
                    {
                        MassageSummery("IIS5" + Session["uInSuc"]);
                    }

                    Session.Remove("uInSuc");
                }

                string postCause = this.Page.Request.Params.Get("__EVENTTARGET");

                if (string.IsNullOrEmpty(postCause))
                {
                    postCause = "REDIRECT";
                }


                //lblError.Text = "";

                GridView gr = (GridView)UCGrid1.FindControl("grdUsers");
                foreach (GridViewRow row in gr.Rows)
                {
                    RadioButton rd = (RadioButton)row.Cells[0].FindControl("RadioButton1");
                    if (rd.Checked == true)
                    {
                        isChecked = 1;
                    }
                }




                if (!String.IsNullOrEmpty(Request.QueryString["x"]))
                {
                    usrType = Request.QueryString["x"].ToString();

                }



                if (usrType == "HQ")
                {

                    if (!IsPostBack)
                    {
                        UCSearch1.Visible = false;
                        UCGrid1.SetText("1");
                        int[] numbers = { 1 };
                        int type = Convert.ToInt32(UserTypesEn.HQ);
                        DataTable use = Source(type, 1);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(type, 2)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);

                        //lblHeader.Text = "Manage HQ users.";
                    }
                }
                else if (usrType == "group")
                {
                    Session["usrType"] = "Manage Group Users";
                    DropDownList drGrp = (DropDownList)UCSearch1.FindControl("drpdownGroup");



                    if (Session["groupId"] != null)
                    {
                        int bId = Convert.ToInt32(Session["groupId"].ToString());
                        int type = Convert.ToInt32(UserTypesEn.Group);



                        int[] numbers = { 1 };


                        if (!postCause.Contains("RadioButton1"))
                        {

                            u = type;
                            n = bId;

                            if (!IsPostBack)
                            {
                                pageNo = 1;
                                UCGrid1.SetText("1");
                                DataTable use = Source(type, bId, 1);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, bId, 2)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else
                            {
                                DataTable use = Source(type, bId, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, bId, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);



                            }
                        }


                        //if (isChecked == 0)
                        //{
                        //    UCGrid1.SetDataSource(Source(type, bId), numbers);
                        //}

                        //ViewState["grp"] = Session["groupId"].ToString();
                        drGrp.SelectedValue = Session["groupId"].ToString();
                        Session.Remove("groupId");
                    }
                    else if (drGrp != null && drGrp.SelectedValue != "-1")
                    {
                        UCGridButtons1.Visible = true;
                        int grpId = Convert.ToInt32(drGrp.SelectedValue);
                        int type = Convert.ToInt32(UserTypesEn.Group);

                        int[] numbers = { 1 };

                        if (!postCause.Contains("RadioButton1"))
                        {
                            u = type;
                            n = grpId;

                            if (!IsPostBack)
                            {
                                pageNo = 1;
                                UCGrid1.SetText("1");
                                DataTable use = Source(type, grpId, 1);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, grpId, 2)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else
                            {
                                DataTable use = Source(type, grpId, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, grpId, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }



                        }

                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        UCGrid1.IsLastPage = true;
                        UCGrid1.SetDataSource(dt);
                        UCGridButtons1.Visible = false;
                        btnDisable.Visible = false;
                    }


                    if (drGrp.SelectedValue != "-1")
                    {
                        Session["gr"] = drGrp.SelectedValue;
                    }

                }
                else if (usrType == "b")
                {

                    UCGridButtons1.Visible = true;
                    Session["usrType"] = "Manage Branch Users";

                    DropDownList drGroup = (DropDownList)UCSearch1.FindControl("drpdownGroup");
                    DropDownList drBrnch = (DropDownList)UCSearch1.FindControl("drpdownBranch");



                    if (Page.Request.Params["bId_m_b"] != null)
                    {


                        StructureFacade sFac = new StructureFacade();

                        int bId = Convert.ToInt32(Page.Request.Params["bId_m_b"].ToString());
                        int type = Convert.ToInt32(UserTypesEn.Branch);
                        Branch brs = sFac.GetBranch(bId);
                        int gId = brs.GroupId;


                        int[] numbers = { 1 };

                        System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");

                        if (!postCause.Contains("RadioButton1"))
                        {
                            u = type;
                            n = bId;

                            if (!IsPostBack)
                            {
                                pageNo = 1;
                                UCGrid1.SetText("1");
                                DataTable use = Source(type, bId, 1);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, bId, 2)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else
                            {
                                DataTable use = Source(type, bId, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, bId, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }



                        }

                        //if (isChecked == 0)
                        //{
                        //    UCGrid1.SetDataSource(Source(type, bId), numbers);
                        //}

                        drGroup.SelectedValue = gId.ToString();

                        //populate branches for the selected group
                        List<Branch> br = sFac.GetBranchesForGroup(gId);
                        drBrnch.DataSource = br;
                        Branch brSel = new Branch();
                        brSel.BranchId = -1;
                        brSel.BranchName = "Select...";
                        br.Insert(0, brSel);
                        drBrnch.DataTextField = "BranchName";
                        drBrnch.DataValueField = "BranchId";
                        drBrnch.DataBind();
                        //
                        drBrnch.SelectedValue = bId.ToString();
                        Session.Remove("branchId");


                    }

                    else if (drBrnch != null && drBrnch.SelectedValue != "-1")
                    {
                        int brnchId = Convert.ToInt32(drBrnch.SelectedValue);
                        int type = Convert.ToInt32(UserTypesEn.Branch);

                        int[] numbers = { 1 };
                        System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");

                        if (!postCause.Contains("RadioButton1"))
                        {
                            u = type;
                            n = brnchId;

                            if (!IsPostBack)
                            {
                                pageNo = 1;
                                UCGrid1.SetText("1");
                                
                            }
                            else
                            {
                                DataTable use = Source(type, brnchId, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, brnchId, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                                

                            }


                        }



                    }
                    else
                    {
                        UCGridButtons1.Visible = false;
                        DataTable dt = new DataTable();
                        UCGrid1.SetDataSource(dt);
                        UCGridButtons1.Visible = false;
                        btnDisable.Visible = false;
                    }


                    if (drGroup.SelectedValue != "-1" && drBrnch.SelectedValue != "-1")
                    {
                        Session["gr"] = drGroup.SelectedValue;
                        Session["br"] = drBrnch.SelectedValue;
                    }
                }
                else if (usrType == "u")
                {
                    UCGridButtons1.Visible = true;
                    Session["usrType"] = "Manage Unit Users";
                    DropDownList drUnit = (DropDownList)UCSearch1.FindControl("drpdownUnit");
                    DropDownList drGroup = (DropDownList)UCSearch1.FindControl("drpdownGroup");
                    DropDownList drBrnch = (DropDownList)UCSearch1.FindControl("drpdownBranch");


                    if (Page.Request.Params["uId_m_u"] != null)
                    {
                        int uId = Convert.ToInt32(Page.Request.Params["uId_m_u"].ToString());
                        int type = Convert.ToInt32(UserTypesEn.Unit);
                        StructureFacade sFac = new StructureFacade();
                        SalesOneUnit unt = sFac.GetUnit(uId);
                        int bId = unt.BranchId;
                        Branch brs = sFac.GetBranch(bId);
                        int gId = brs.GroupId;

                        int[] numbers = { 1 };

                        System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");

                        if (!postCause.Contains("RadioButton1"))
                        {
                            u = type;
                            n = uId;

                            if (!IsPostBack)
                            {
                                pageNo = 1;
                                UCGrid1.SetText("1");
                                DataTable use = Source(type, uId, 1);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, uId, 1 + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else
                            {
                                DataTable use = Source(type, uId, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, uId, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }


                        }

                        //if (isChecked == 0)
                        //{
                        //    UCGrid1.SetDataSource(Source(type, uId), numbers);
                        //}


                        drGroup.SelectedValue = gId.ToString();

                        //populate branches for the selected group
                        List<Branch> br = sFac.GetBranchesForGroup(gId);
                        drBrnch.DataSource = br;
                        Branch brSel = new Branch();
                        brSel.BranchId = -1;
                        brSel.BranchName = "Select One...";
                        br.Insert(0, brSel);
                        drBrnch.DataTextField = "BranchName";
                        drBrnch.DataValueField = "BranchId";
                        drBrnch.DataBind();
                        //
                        drBrnch.SelectedValue = bId.ToString();

                        //
                        List<SalesOneUnit> un = sFac.GetUnitsInBranch(bId);
                        SalesOneUnit unSel = new SalesOneUnit();
                        unSel.UnitId = -1;
                        unSel.Name = "Select One...";
                        un.Insert(0, unSel);
                        drUnit.DataSource = un;
                        drUnit.DataTextField = "Name";
                        drUnit.DataValueField = "UnitId";
                        drUnit.DataBind();
                        //
                        drUnit.SelectedValue = uId.ToString();


                        Session.Remove("unitId");



                    }
                    else if (drUnit != null && drUnit.SelectedValue != "-1")
                    {
                        int unitId = Convert.ToInt32(drUnit.SelectedValue);
                        int type = Convert.ToInt32(UserTypesEn.Unit);


                        int[] numbers = { 1 };

                        System.Web.UI.HtmlControls.HtmlInputHidden loadGrd = (System.Web.UI.HtmlControls.HtmlInputHidden)UCSearch1.FindControl("_SETGRIDSOURCE");

                        if (!postCause.Contains("RadioButton1"))
                        {
                            u = type;
                            n = unitId;

                            if (!IsPostBack)
                            {
                                pageNo = 1;
                                UCGrid1.SetText("1");
                                DataTable use = Source(type, unitId, 1);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, unitId, 1 + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else
                            {
                                DataTable use = Source(type, unitId, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, unitId, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }


                        }

                        //if (isChecked == 0)
                        //{
                        //    UCGrid1.SetDataSource(Source(type, unitId), numbers);
                        //}



                    }
                    else
                    {
                        UCGridButtons1.Visible = false;
                        DataTable dt = new DataTable();
                        UCGrid1.IsLastPage = true;
                        UCGrid1.SetDataSource(dt);
                        UCGridButtons1.Visible = false;
                        btnDisable.Visible = false;
                    }


                    if (drGroup.SelectedValue != "-1" && drBrnch.SelectedValue != "-1" && drUnit.SelectedValue != "-1")
                    {
                        Session["gr"] = drGroup.SelectedValue;
                        Session["br"] = drBrnch.SelectedValue;
                        Session["un"] = drUnit.SelectedValue;
                    }

                }
            }
            catch (Exception)
            {
                

            }

            
        }


        private DataTable Source(int userType, int nodeId,int pageNo)
        {
            try
            {
                StructureFacade fac = new StructureFacade();

                DataTable usrTable = new DataTable();
                DataRow newRow;

                usrTable.Columns.Add("Id");
                usrTable.Columns.Add("Name");
                usrTable.Columns.Add("Last Updated By");
                usrTable.Columns.Add("Last Updated Date");


                foreach (User usr in fac.GetUsersInStructure(userType, nodeId, pageNo))
                {
                    string lastUpBy = "";
                    string updatedDate = "";
                    string name = "";

                    if (usr != null)
                    {
                        MembershipUser u = Membership.GetUser(usr.UserName);

                        if (usr.LastUpBy != string.Empty)
                        {
                            MembershipUser adUser = Membership.GetUser(usr.LastUpBy);
                            User adObj = fac.GetUser(usr.LastUpBy);

                            lastUpBy = adObj.Name;
                            updatedDate = usr.LastUpdateDate;
                            name = usr.Name;
                        }

                        newRow = usrTable.NewRow();
                        newRow[0] = usr.UserName;
                        newRow[1] = name;
                        newRow[2] = lastUpBy;
                        newRow[3] = updatedDate;

                        if (u.IsApproved != false && u.IsLockedOut != true)
                        {
                            usrTable.Rows.Add(newRow);
                        }

                    }


                }

                if (usrTable.Rows.Count > 0)
                {
                    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                    btnUsrCreate.Visible = false;
                    btnDisable.Visible = true;
                }
                else
                {
                    MassageSummery("IIS504");
                    //lblError.Text = "No users to display";
                    btnUsrCreate.Visible = true;
                    UCGridButtons1.Visible = false;
                    btnDisable.Visible = false;
                }

                return usrTable;
            }
            catch (Exception)
            {
                return null;
            }
            
        }



        private DataTable Source(int userType,int pageNo)
        {

            try
            {
                StructureFacade fac = new StructureFacade();

                DataTable usrTable = new DataTable();
                DataRow newRow;

                usrTable.Columns.Add("Id");
                usrTable.Columns.Add("Name");
                usrTable.Columns.Add("Last Updated By");
                usrTable.Columns.Add("Last Updated Date");


                foreach (User usr in fac.GetUsersInHQ(userType, pageNo))
                {
                    string lastUpBy = "";
                    string updatedDate = "";
                    string name = "";


                    if (usr != null)
                    {
                        MembershipUser u = Membership.GetUser(usr.UserName);

                        if (usr.LastUpBy != string.Empty)
                        {
                            MembershipUser adUser = Membership.GetUser(usr.LastUpBy);
                            User adObj = fac.GetUser(usr.LastUpBy);

                            lastUpBy = adObj.Name;
                            updatedDate = usr.LastUpdateDate;
                            name = usr.Name;
                        }


                        newRow = usrTable.NewRow();
                        newRow[0] = usr.UserName;
                        newRow[1] = name;
                        newRow[2] = lastUpBy;
                        newRow[3] = updatedDate;

                        if (u.IsApproved != false && u.IsLockedOut != true)
                        {
                            usrTable.Rows.Add(newRow);
                        }

                    }

                }

                if (usrTable.Rows.Count > 0)
                {
                    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                    btnUsrCreate.Visible = false;
                    btnDisable.Visible = true;
                    UCGridButtons1.Visible = true;
                }
                else
                {
                    btnUsrCreate.Visible = true;
                    btnDisable.Visible = false;
                    UCGridButtons1.Visible = false;
                }

                return usrTable;
            }
            catch (Exception)
            {
                return null;
            }

            


        }

        public void View()
        {
            try
            {
                if (Session["userName"] == null)
                {

                    if (Session["firstRow"] != null)
                    {
                        Session["userName"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                        Session.Remove("firstRow");
                    }
                    else
                    {
                        MassageSummery("IIS503");
                    }

                }


                if (Session["userName"] != null)
                {
                    Response.Redirect("~/UserManagement/ViewUser.aspx");
                }
                else
                {
                    MassageSummery("IIS503");
                    //lblError.Text = "Please select a user";
                }
            }
            catch (Exception)
            {

            }
            

            
        }

        public void Create()
        {
            try
            {
                if (!String.IsNullOrEmpty(Request.QueryString["x"]))
                {
                    usrType = Request.QueryString["x"].ToString();
                    Response.Redirect("~/UserManagement/AddUser.aspx?x=" + usrType);

                }
            }
            catch (Exception)
            {

            }
            
            //Response.Redirect("~/UserManagement/AddUser.aspx");

        }

        public void Update()
        {
            try
            {
                if (Session["userName"] == null)
                {
                    if (Session["firstRow"] != null)
                    {
                        Session["userName"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                        Session.Remove("firstRow");
                    }
                    else
                    {
                        MassageSummery("IIS503");
                    }
                }


                if (Session["userName"] != null)
                {
                    Response.Redirect("~/UserManagement/UpdateUser.aspx");
                }
                else
                {
                    MassageSummery("IIS503");
                    //lblError.Text = "Please select a user";
                }
            }
            catch (Exception)
            {
                
            }
            
        }

        public void Delete()
        {
            try
            {
                if (Session["userName"] == null)
                {
                    if (Session["firstRow"] != null)
                    {

                        Session["userName"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                        Session.Remove("firstRow");
                    }
                    else
                    {
                        MassageSummery("IIS503");
                    }
                }



                if (Session["userName"] != null)
                {


                    int[] numbers = { 1 };
                    string user = Session["userName"].ToString();

                    MembershipUser usr = Membership.GetUser(user);
                    usr.IsApproved = false;
                    Membership.UpdateUser(usr);

                    Guid userGuuid = (Guid)usr.ProviderUserKey;
                    

                    StructureFacade StructureFacade = new StructureFacade();

                    User usrObj = StructureFacade.GetUser(userGuuid);
                    usrObj.Country = "1";
                    usrObj.IdType = "1";
                    

                    UserInformation info = StructureFacade.GetUserInformation(Session["userName"].ToString());

                    int type = 0;
                    int node = 0;

                    if (!String.IsNullOrEmpty(Request.QueryString["x"]))
                    {
                        usrType = Request.QueryString["x"].ToString();

                    }

                    int success = 0;

                    usrObj.IsDeleted = true;
                    success = StructureFacade.UpdateUser(usrObj);

                    if (success == 1)
                    {
                        //bool f = Membership.DeleteUser(usr.UserName, true);

                            if (usrType == "HQ")
                            {
                                type = Convert.ToInt32(UserTypesEn.HQ);

                                DataTable use = Source(type, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else if (usrType == "group")
                            {
                                type = Convert.ToInt32(UserTypesEn.Group);
                                node = (int)info.GroupId;

                                DataTable use = Source(type, node, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else if (usrType == "b")
                            {
                                type = Convert.ToInt32(UserTypesEn.Branch);
                                node = (int)info.BranchId;

                                DataTable use = Source(type, node, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }
                            else if (usrType == "u")
                            {
                                type = Convert.ToInt32(UserTypesEn.Unit);
                                node = (int)info.UnitId;

                                DataTable use = Source(type, node, pageNo);
                                if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                                }
                                UCGrid1.SetDataSource(use, numbers);
                            }

                            //lblSuccess.Text = "Successfully deleted user " + usr.UserName;
                            MassageSummery("IIS401");
                        

                    }
                    else
                    {
                        MassageSummery("IIS502");
                        //lblError.Text = "Fail to delete user";
                    }

                    Session.Remove("userName");

                    //int success = 0;
                    //success = StructureFacade.DeleteUser(usrObj);
                    //if (success == 1)
                    //{
                    //    bool f = Membership.DeleteUser(usr.UserName, true);
                    //    if (f == true)
                    //    {

                    //        if (usrType == "HQ")
                    //        {
                    //            type = Convert.ToInt32(UserTypesEn.HQ);
                    //            UCGrid1.SetDataSource(Source(type, pageNo), numbers);
                    //        }
                    //        else if (usrType == "group")
                    //        {
                    //            type = Convert.ToInt32(UserTypesEn.Group);
                    //            node = (int)info.GroupId;
                    //            UCGrid1.SetDataSource(Source(type, node, pageNo), numbers);
                    //        }
                    //        else if (usrType == "b")
                    //        {
                    //            type = Convert.ToInt32(UserTypesEn.Branch);
                    //            node = (int)info.BranchId;
                    //            UCGrid1.SetDataSource(Source(type, node, pageNo), numbers);
                    //        }
                    //        else if (usrType == "u")
                    //        {
                    //            type = Convert.ToInt32(UserTypesEn.Unit);
                    //            node = (int)info.UnitId;
                    //            UCGrid1.SetDataSource(Source(type, node, pageNo), numbers);
                    //        }

                    //        //lblSuccess.Text = "Successfully deleted user " + usr.UserName;
                    //        MassageSummery("IIS401");
                    //    }

                    //}
                    //else
                    //{
                    //    MassageSummery("IIS502");
                    //    //lblError.Text = "Fail to delete user";
                    //}

                    //Session.Remove("userName");


                }
                else
                {
                    MassageSummery("IIS503");
                    //lblError.Text = "Please select a user";
                }
               

            }
            catch (Exception)
            {

            }
            

        }

        public void Cancel()
        {

        }
        public void ManageUser()
        {
            //throw new NotImplementedException();
        }

        public void ManageUnit()
        {
            //throw new NotImplementedException();
        }






        public void GetId(GridViewRow row)
        {
            Session["userName"] = row.Cells[2].Text;
        }

        protected void btnUsrCreate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["x"]))
            {
                usrType = Request.QueryString["x"].ToString();
                Response.Redirect("~/UserManagement/AddUser.aspx?x=" + usrType);

            }

            
        }

        protected void btnDisable_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["userName"] == null)
                {
                    if (Session["firstRow"] != null)
                    {
                        Session["userName"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                        Session.Remove("firstRow");
                    }
                    else
                    {
                        MassageSummery("IIS503");
                    }
                    
                }


                if (Session["userName"] != null)
                {
                    int[] numbers = { 1 };
                    MembershipUser usr = Membership.GetUser(Session["userName"].ToString());
                    usr.IsApproved = false;
                    Membership.UpdateUser(usr);
                    StructureFacade sFc = new StructureFacade();
                    UserInformation info = sFc.GetUserInformation(Session["userName"].ToString());

                    int type = 0;
                    int node = 0;

                    if (!String.IsNullOrEmpty(Request.QueryString["x"]))
                    {
                        usrType = Request.QueryString["x"].ToString();

                    }

                    if (usrType == "HQ")
                    {
                        type = Convert.ToInt32(UserTypesEn.HQ);

                        DataTable use = Source(type, pageNo);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(type, pageNo + 1)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);
                    }
                    else if (usrType == "group")
                    {
                        type = Convert.ToInt32(UserTypesEn.Group);
                        node = (int)info.GroupId;

                        DataTable use = Source(type, node, pageNo);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);
                    }
                    else if (usrType == "b")
                    {
                        type = Convert.ToInt32(UserTypesEn.Branch);
                        node = (int)info.BranchId;

                        DataTable use = Source(type, node, pageNo);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);
                    }
                    else if (usrType == "u")
                    {
                        type = Convert.ToInt32(UserTypesEn.Unit);
                        node = (int)info.UnitId;

                        DataTable use = Source(type, node, pageNo);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);
                    }

                    Session.Remove("userName");


                    MassageSummery("IIS406");

                }
                else
                {
                    MassageSummery("IIS503");
                    //lblError.Text = "Please select a user";
                }
            }
            catch (Exception)
            {
                MassageSummery("IIS502");
            }

            
            
        }

        //protected void Page_Unload(object sender, EventArgs e)
        //{
        //    Session["groupId"] = null;
        //}

        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    this.Unload += new EventHandler(OnUnload);

        //}

        //protected override void OnUnload(EventArgs e)
        //{
        //    Session["groupId"] = null;
        //    base.OnUnload(e);
        //}


        #region IntTest Members

        public void Next()
        {


            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                UCGrid1.SetText(pageNo.ToString());

            }
            else
            {
                pageNo = 2;
                UCGrid1.SetText("2");

            }

            int[] numbers = { 1 };
            int type= u;
            int node = n;


            if (!String.IsNullOrEmpty(Request.QueryString["x"]))
            {
                usrType = Request.QueryString["x"].ToString();

            }


            if (usrType == "HQ")
            {
                //if (Source(1,pageNo).Rows.Count >0)
                //{
                    DataTable use = Source(1, pageNo);
                    if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(1, pageNo + 1)).Rows.Count > 0 ? false : true;
                    }
                    UCGrid1.SetDataSource(use,numbers);
                //}
                //else
                //{
                   // UCGrid1.SetDataSource
                //}
                
            }
            else if (usrType == "group")
            {
                //if (Source(type, node, pageNo).Rows.Count > 0)
                //{
                    DataTable use = Source(type, node, pageNo);
                    if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                    }
                    UCGrid1.SetDataSource(use, numbers);
                //}
            }
            else if (usrType == "b")
            {
                //if (Source(type, node, pageNo).Rows.Count > 0)
                //{
                    DataTable use = Source(type, node, pageNo);
                    if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                    }
                    UCGrid1.SetDataSource(use, numbers);
                //}
            }
            else if (usrType == "u")
            {
                //if (Source(type, node, pageNo).Rows.Count > 0)
                //{
                    DataTable use = Source(type, node, pageNo);
                    if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                    }
                    UCGrid1.SetDataSource(use, numbers);
                //}
            }
        }

        public void Previous()
        {

            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                UCGrid1.SetText(pageNo.ToString());


                if (!String.IsNullOrEmpty(Request.QueryString["x"]))
                {
                    usrType = Request.QueryString["x"].ToString();

                }

                int[] numbers = { 1 };
                int type = u;
                int node = n;


                if (usrType == "HQ")
                {
                    DataTable use = Source(1, pageNo);
                    if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(1, pageNo + 1)).Rows.Count > 0 ? false : true;
                    }
                    UCGrid1.SetDataSource(use, numbers);
                }
                else if (usrType == "group")
                {
                    if (Source(type, node, pageNo).Rows.Count > 0)
                    {
                        DataTable use = Source(type, node, pageNo);
                    if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                    }
                        UCGrid1.SetDataSource(use, numbers);
                    }
                }
                else if (usrType == "b")
                {
                    if (Source(type, node, pageNo).Rows.Count > 0)
                    {
                        DataTable use = Source(type, node, pageNo);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);
                    }
                }
                else if (usrType == "u")
                {
                    if (Source(type, node, pageNo).Rows.Count > 0)
                    {
                        DataTable use = Source(type, node, pageNo);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(type, node, pageNo + 1)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);
                    }
                }



            }

        }

        public void Go()
        {
            if (!String.IsNullOrEmpty(Request.QueryString["x"]))
            {
                usrType = Request.QueryString["x"].ToString();

            }

            int[] numbers = { 1 };
            int type = u;
            int node = n;

            int p = int.Parse(UCGrid1.GetText());
            if (p != 0)
            {
                try
                {
                    if (usrType == "HQ")
                    {
                        DataTable use = Source(1, p);
                        if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                        {
                            UCGrid1.IsLastPage = (Source(1, p + 1)).Rows.Count > 0 ? false : true;
                        }
                        UCGrid1.SetDataSource(use, numbers);
                    }
                    else if (usrType == "group")
                    {
                        if (Source(type, node, p).Rows.Count >0)
                        {
                            DataTable use = Source(type, node, p);
                            if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                            {
                                UCGrid1.IsLastPage = (Source(type, node, p + 1)).Rows.Count > 0 ? false : true;
                            }
                            UCGrid1.SetDataSource(use, numbers);
                        }
                        
                    }
                    else if (usrType == "b")
                    {
                        if (Source(type, node, p).Rows.Count > 0)
                        {
                            DataTable use = Source(type, node, p);
                            if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                            {
                                UCGrid1.IsLastPage = (Source(type, node, p + 1)).Rows.Count > 0 ? false : true;
                            }
                            UCGrid1.SetDataSource(use, numbers);
                        }
                    }
                    else if (usrType == "u")
                    {
                        if (Source(type, node, p).Rows.Count > 0)
                        {
                            DataTable use = Source(type, node, p);
                            if (use.Rows.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                            {
                                UCGrid1.IsLastPage = (Source(type, node, p + 1)).Rows.Count > 0 ? false : true;
                            }
                            UCGrid1.SetDataSource(use, numbers);
                        }
                    }



                    pageNo = p;
                }
                catch (Exception)
                {

                    throw;
                }


            }
        }

        #endregion
    }
}
